﻿using System;
using System.IO;
using System.Web;
using ECGadmin.Utility;
using ECGadmin.WebUI.Infrastructure;

namespace ECGadmin.WebUI.CustomHelpers
{
    public class ImageHelper
    {
        /// <summary>
        /// Upload ảnh đại diện cho sản phẩm
        /// </summary>
        /// <param name="error"></param>
        /// <param name="imageFile"></param>
        /// <returns></returns>
        public static string UploadProductImage(out string error,  HttpPostedFileBase imageFile)
        {
            var isValid = true;
            var e = "";
            var filePathSaved = string.Empty;
            var currentDate = DateTime.Now.ToString("dd/MM/yyyy");

            var folderRelative = ConstParam.FolderUploadImageRoot + "/" + currentDate;
            var folderUpload = CreatePhysicalFolderPath(folderRelative);
           
            var imgContentType = imageFile.ContentType;

            if (imgContentType.Equals("image/png") && imgContentType.Equals("image/jpeg") && imgContentType.Equals("image/gif"))
            {
                e = "Bạn phải sử dụng file ảnh có định dạng jpg, png, gif.";
                isValid = false;
            }

            if (isValid)
            {
                try
                {
                    filePathSaved = folderRelative + "/"  + UploadImage(folderUpload, imageFile);
                }
                catch (Exception ex)
                {
                    e = ex.Message;
                }
            }
            error = e;
            return filePathSaved;
        }

        /// <summary>
        /// Upload ảnh đại diện cho sản phẩm và xóa ảnh cũ nếu có
        /// </summary>
        /// <param name="error"></param>
        /// <param name="imageFile"></param>
        /// <param name="orderImagePath"></param>
        /// <returns></returns>
        public static string UploadProductImage(out string error, HttpPostedFileBase imageFile, string orderImagePath)
        {
            string errorUploadImg = "";

            // Xóa file ảnh nếu có
            var physicalImgPath = CreatePhysicalFolderPath(orderImagePath);
            if (File.Exists(physicalImgPath)) File.Delete(physicalImgPath);
            var filePathSaved = UploadProductImage(out errorUploadImg, imageFile);

            error = errorUploadImg;
            return filePathSaved;
        }

        /// <summary>
        /// Tạo tên thư mục lưu ảnh có đường dẫn theo ngày tháng
        /// </summary>
        /// <param name="folderRoot"></param>
        /// <returns></returns>
        public static string CreatePhysicalFolderPath(string folderRoot)
        {
            // Create sub folder by datetime
            var fullFolder = HttpContext.Current.Server.MapPath(folderRoot);
            return fullFolder;
        }

        /// <summary>
        /// Upload image
        /// </summary>
        /// <param name="folderUpload"></param>
        /// <param name="imageFile"></param>
        /// <returns></returns>
        public static string UploadImage(string folderUpload, HttpPostedFileBase imageFile)
        {
            var fileName = imageFile.FileName;
            var extension = Path.GetExtension(imageFile.FileName) ?? "";
            fileName = Path.GetFileName(string.Format("{0}-{1}.{2}", StringUtil.RemoveSpecialCharactersVn(fileName.Replace(extension, "")).GetNormalizeString(), Utility.Common.GenerateId(), "jpg"));
            if (!Directory.Exists(folderUpload)) Directory.CreateDirectory(folderUpload);

            imageFile.SaveAs(Path.Combine(folderUpload, fileName ?? ""));

            return  fileName;
        }

        /// <summary>
        /// Update Image Common
        /// </summary>
        /// <param name="error"></param>
        /// <param name="folderUpload"></param>
        /// <param name="imageFile"></param>
        /// <returns></returns>
        public static string UploadImageCommon(out string error, string folderUpload,  HttpPostedFileBase imageFile)
        {
            var isValid = true;
            var e = "";
            var filePathSaved = string.Empty;
            var currentDate = DateTime.Now.ToString("dd/MM/yyyy");

            var folderRelative = folderUpload + "/" + currentDate;
            var folderUploadPhysical = CreatePhysicalFolderPath(folderRelative);

            var imgContentType = imageFile.ContentType;

            if (imgContentType.Equals("image/png") && imgContentType.Equals("image/jpeg") && imgContentType.Equals("image/gif"))
            {
                e = "Bạn phải sử dụng file ảnh có định dạng jpg, png, gif.";
                isValid = false;
            }

            if (isValid)
            {
                try
                {
                    filePathSaved = folderRelative + "/" + UploadImage(folderUploadPhysical, imageFile);
                }
                catch (Exception ex)
                {
                    e = ex.Message;
                }
            }
            error = e;
            return filePathSaved;
        }

        /// <summary>
        /// Upload ảnh mới và xóa ảnh cũ
        /// </summary>
        /// <param name="error"></param>
        /// <param name="folderUpload"></param>
        /// <param name="imageFile"></param>
        /// <param name="ordeImagePath"></param>
        /// <returns></returns>
        public static string UploadImageCommon(out string error, string folderUpload, HttpPostedFileBase imageFile, string ordeImagePath)
        {
            var isValid = true;
            var e = "";
            var filePathSaved = string.Empty;
            var currentDate = DateTime.Now.ToString("dd/MM/yyyy");

            var folderRelative = folderUpload + "/" + currentDate;
            var folderUploadPhysical = CreatePhysicalFolderPath(folderRelative);

            var imgContentType = imageFile.ContentType;

            if (imgContentType.Equals("image/png") && imgContentType.Equals("image/jpeg") && imgContentType.Equals("image/gif"))
            {
                e = "Bạn phải sử dụng file ảnh có định dạng jpg, png, gif.";
                isValid = false;
            }

            if (isValid)
            {
                try
                {
                    filePathSaved = folderRelative + "/" + UploadImage(folderUploadPhysical, imageFile);
                    DeleteImageFile(ordeImagePath);
                }
                catch (Exception ex)
                {
                    e = ex.Message;
                }
            }
            error = e;
            return filePathSaved;
        }

        /// <summary>
        /// Xóa file ảnh nếu có
        /// </summary>
        /// <param name="imagePath"></param>
        public static void DeleteImageFile(string imagePath)
        {
            // Xóa file ảnh nếu có
            var physicalImgPath = CreatePhysicalFolderPath(imagePath);
            if (File.Exists(physicalImgPath)) File.Delete(physicalImgPath);
        }
    }
}